﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Resources;
using System.Reflection;
using Sherwood.SignOn.Server.Models.Services;
using Sherwood.SignOn.Server.Content;

namespace Sherwood.SignOn.Server.Controllers
{
    public class ControllerBase : Controller
    {
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            IUserSession session = UserSessionHelper.GetCurrentUserSession();
            if (session != null && session.UserAccount != null)
            {
                SetCulture(session.UserAccount.CultureName1, session.UserAccount.CultureName2,
                           session.UserAccount.CultureName3);
            }
            else
            {
                string browserLanguages = requestContext.HttpContext.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
                if (browserLanguages != null)
                {
                    string primaryLanguage = browserLanguages.Split(',')[0];
                    if (primaryLanguage.Length > 0)
                    {
                        try
                        {
                            SetCulture(primaryLanguage.Trim());
                        }
                        catch { }
                    }
                }

            }

            base.Initialize(requestContext);
        }

        protected override void Execute(System.Web.Routing.RequestContext requestContext)
        {

            base.Execute(requestContext);

        }

        public static void SetCulture(params string[] cultures)
        {
            foreach (string culture in cultures)
            {
                try
                {
                    CultureInfo inf = new CultureInfo(culture);
                    ResourceSet resources = Translations.ResourceManager.GetResourceSet(inf, false, true);
                    if (resources != null)
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture = inf;
                        if (!inf.IsNeutralCulture)
                            System.Threading.Thread.CurrentThread.CurrentUICulture = inf;
                        return;
                    }
                }
                catch { }

            }
        }


        /*protected ActionResult NoCacheRedirect(string url)
        {
            HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Response.Redirect(url);
            return Content("");
        }*/

      


    }
}
